﻿using ProjectFootball.MVC.Areas.MatchArea.Models;
using ProjectFootball.MVC.Foundation.ResourceLibrary;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;

namespace ProjectFootball.MVC.Models.EntityFramework.Entities
{
    public static class _MatchExtensions
    {
        public static MatchDataModel GetDataModel(this Match match)
        {
            return new MatchDataModel()
            {
                MatchGuid = match.Pk,
                ApprovalGuid = match.ApprovalGuid,
                MatchDate = match.MatchDate,
                CompetitionGuid = match.CompetitionGuid,
                VenueGuid = match.VenueGuid,
                Attendance = match.Attendance,
                Team1Guid = match.Team1Guid,
                Team1HT = match.Team1HT,
                Team1FT = match.Team1FT,
                Team1ETHT = match.Team1ETHT,
                Team1ETFT = match.Team1ETFT,
                Team1Pens = match.Team1Pens,
                Team2Guid = match.Team2Guid,
                Team2HT = match.Team2HT,
                Team2FT = match.Team2FT,
                Team2ETHT = match.Team2ETHT,
                Team2ETFT = match.Team2ETFT,
                Team2Pens = match.Team2Pens
            };
        }

        public static IEnumerable<MatchSearchResultViewModel> Search(this IEnumerable<Match> matches, IPrincipal user, SearchType searchType)
        {
            foreach (var match in matches)
                yield return match.GetSearchResultViewModel(user, searchType);
        }

        public static MatchSearchResultViewModel GetSearchResultViewModel(this Match match, IPrincipal user, SearchType searchType)
        {
            return new MatchSearchResultViewModel()
            {
                Match = match,
                IsVisible = match.CanView(user),
                IsEditVisible = searchType == SearchType.Details ? match.CanView(user) : false,
                IsDeleteVisible = (searchType == SearchType.Details || (searchType == SearchType.RecordForApproval && !match.IsApproved)) ? match.CanDelete(user) : false,
                IsViewApprovalsVisible = (searchType == SearchType.Details || searchType == SearchType.PendingApprovals) ? match.CanApprove(user) : false,
                IsConfirmApprovalVisible = searchType == SearchType.RecordForApproval ? match.CanApprove(user) : false,
                CanImport = searchType == SearchType.Import && user.IsInRole("Admin")
            };
        }

        public static void Edit(this Match match, MatchDataModel matchDataModel, IPrincipal user)
        {
            match.ApprovalGuid = matchDataModel.ApprovalGuid;
            match.MatchDate = matchDataModel.MatchDate;
            match.CompetitionGuid = matchDataModel.CompetitionGuid;
            match.VenueGuid = matchDataModel.VenueGuid;
            match.Attendance = matchDataModel.Attendance;
            match.Team1Guid = matchDataModel.Team1Guid;
            match.Team1HT = matchDataModel.Team1HT;
            match.Team1FT = matchDataModel.Team1FT;
            match.Team1ETHT = matchDataModel.Team1ETHT;
            match.Team1ETFT = matchDataModel.Team1ETFT;
            match.Team1Pens = matchDataModel.Team1Pens;
            match.Team2Guid = matchDataModel.Team2Guid;
            match.Team2HT = matchDataModel.Team2HT;
            match.Team2FT = matchDataModel.Team2FT;
            match.Team2ETHT = matchDataModel.Team2ETHT;
            match.Team2ETFT = matchDataModel.Team2ETFT;
            match.Team2Pens = matchDataModel.Team2Pens;
            match.IsApproved = match.IsApproved ? user.IsApprover() : false;
            match.AuditUserId = user.GetUserId();
            match.AuditTimestamp = DateTime.Now;
        }

        public static void Approve(this Match match, Match previousApproved)
        {
            previousApproved.MatchDate = match.MatchDate;
            previousApproved.CompetitionGuid = match.CompetitionGuid;
            previousApproved.VenueGuid = match.VenueGuid;
            previousApproved.Attendance = match.Attendance;
            previousApproved.Team1Guid = match.Team1Guid;
            previousApproved.Team1HT = match.Team1HT;
            previousApproved.Team1FT = match.Team1FT;
            previousApproved.Team1ETHT = match.Team1ETHT;
            previousApproved.Team1ETFT = match.Team1ETFT;
            previousApproved.Team1Pens = match.Team1Pens;
            previousApproved.Team2Guid = match.Team2Guid;
            previousApproved.Team2HT = match.Team2HT;
            previousApproved.Team2FT = match.Team2FT;
            previousApproved.Team2ETHT = match.Team2ETHT;
            previousApproved.Team2ETFT = match.Team2ETFT;
            previousApproved.Team2Pens = match.Team2Pens;
            previousApproved.AuditUserId = match.AuditUserId;
            previousApproved.AuditTimestamp = match.AuditTimestamp;
        }
    }
}